package lib.pacificapp.app;

import java.util.LinkedList;
import java.util.List;

import lib.pacificapp.app.model.Container;
 
import android.app.Activity;
import android.util.Log;

public class ActionContainer {
	
	 private static ActionContainer _intance;
	 public static ActionContainer getInstance(){
		 if(_intance==null)
		    _intance=new ActionContainer();
		    return _intance;
		 
	 }
	 
     private List<Container>  activityList;
     public ActionContainer(){
		  activityList = new LinkedList<Container>();
	  }
     public   void push(Activity activity)
 	{
 		   activityList.add(new Container("action_"+activityList.size(),activity));
 	} 
	 public   void push(String name,Activity activity)
	{
		   activityList.add(new Container(name,activity));
	} 
	 public   void clean()
		{
		 clean("");
		}
    public   void clean(String widthOut)
	{
        for(Container activity:activityList)
	    {
        	if(!activity.getActionName().equals(widthOut))
	            activity.getAction().finish();
	    }
        if(widthOut.equals("")){
          android.os.Process.killProcess(android.os.Process.myPid());  
          System.exit(10);
        }
      
    }
    public   void cleanTo(String toAction)
   	{
    	
    	  	  
    	  for(int i=activityList.size();i>0;i--)
   	    {
    		  Container activity=activityList.get(i-1);
    		  Log.d("test",toAction+"?="+activity.getActionName());
   	          	if(activity.getActionName().equals(toAction)){
   	           return ;
   	        }else{
   	            activity.getAction().finish();
   	        }
        }
   	}
    public  Activity getAction(String toAction)
   	{
    	
    	  	  
    	  for(int i=activityList.size();i>0;i--)
   	    {
    		  Container activity=activityList.get(i-1);
    		  if(activity.getActionName().equals(toAction)){
   	           return activity.getAction();
   	         } 
        }
    	  return null;
   	}
}
